// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.parts;

import org.dengues.reports.editor.design.layers.TableBorderLayer;
import org.dengues.reports.editor.design.layout.ITableLayoutOwner;
import org.dengues.reports.editor.design.layout.TableLayout;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayeredPane;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayeredPane;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.LayerManager;

/**
 * Abstract class for the table editpart.
 * 
 * /** Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-22 qiang.zhang $
 * 
 */
public abstract class AbstractTableEditPart extends ReportElementEditPart implements LayerConstants, ITableLayoutOwner {

    public static final String BORDER_LAYER = "Table Border layer"; //$NON-NLS-1$

    protected FreeformLayeredPane innerLayers;

    protected LayeredPane printableLayers;

    /**
     * Constractor
     * 
     * @param model
     */
    public AbstractTableEditPart(Object model) {
        super(model);
    }

    /**
     * Returns the layer indicated by the key. Searches all layered panes.
     * 
     * @see LayerManager#getLayer(Object)
     */
    public IFigure getLayer(Object key) {
        if (innerLayers == null)
            return null;
        IFigure layer = innerLayers.getLayer(key);
        if (layer != null)
            return layer;
        if (printableLayers == null)
            return null;
        return printableLayers.getLayer(key);
    }

    /**
     * this layer may be a un-useful layer.
     * 
     * @return the layered pane containing all printable content
     */
    protected LayeredPane getPrintableLayers() {
        if (printableLayers == null)
            printableLayers = createPrintableLayers();
        return printableLayers;
    }

    /**
     * Creates a layered pane and the layers that should be printed.
     * 
     * @see org.eclipse.gef.print.PrintGraphicalViewerOperation
     * @return a new LayeredPane containing the printable layers
     */
    protected LayeredPane createPrintableLayers() {
        FreeformLayeredPane layeredPane = new FreeformLayeredPane();
        FreeformLayer layer = new FreeformLayer();

        layer.setLayoutManager(new TableLayout(this));
        layeredPane.add(layer, PRIMARY_LAYER);
        layeredPane.add(new TableBorderLayer(this), BORDER_LAYER);
        return layeredPane;
    }

    /**
     * The contents' Figure will be added to the PRIMARY_LAYER.
     * 
     * @see org.eclipse.gef.GraphicalEditPart#getContentPane()
     */
    public IFigure getContentPane() {
        return getLayer(PRIMARY_LAYER);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.birt.report.designer.internal.ui.layout.ITableLayoutOwner#reLayout()
     */
    public void reLayout() {
        notifyModelChange();
        getFigure().invalidateTree();
        // getFigure( ).getUpdateManager( ).addInvalidFigure( getFigure( ) );
        getFigure().revalidate();
    }

}
